package dao;




import java.io.*;
import java.sql.*;
import java.util.*;

public class DBManager {

	private static DBManager _instance;
	private static Connection conn = null;
	private FileInputStream in;
	private Properties props;

	private DBManager() {
		if (!dbExists()) {		
			System.err.println("De database bestaat niet");			
		}
	}

	public static synchronized DBManager getInstance() {
		if (_instance == null) {
			_instance = new DBManager();
		}
		return _instance;
	}

	public Connection getConnection() {

		return conn;
	}

	public void close() {

		try {
			conn.close();
			_instance = null;
			conn = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private boolean dbExists() {
		boolean exists = false;

		props = new Properties();
		try {
			in = new FileInputStream("database.properties");
			props.load(in);
			in.close();

			String drivers = props.getProperty("jdbc.drivers");
			if (drivers != null) {
				System.setProperty("jdbc.drivers", drivers);
				String hostname = props.getProperty("jdbc.hostname");
				String database = props.getProperty("jdbc.database");
				String username = props.getProperty("jdbc.username");
				String password = props.getProperty("jdbc.password");
				
				
				if (drivers.equalsIgnoreCase("com.mysql.jdbc.Driver")) {
					conn = DriverManager.getConnection(hostname, username, password);
					String qExists = "SHOW DATABASES LIKE ?";
					String qMakeDB = "CREATE DATABASE ?";
					PreparedStatement st = conn.prepareStatement(qExists);
					PreparedStatement mkdb = conn.prepareStatement(qMakeDB);
					st.setString(1, database);
					mkdb.setString(1, database);
					ResultSet rs = st.executeQuery();
					
					if( rs.next() == false ) {
						// De database bestaat niet, maar de server wel
						exists = false;
						
						// Maak de database aan
						mkdb.execute();
						
						// Verbind met de nieuwe database
						conn = DriverManager.getConnection(hostname+"/"+database, username, password);
												
						exists = true;
					}
					
				} else if (drivers.equalsIgnoreCase("org.postgresql.Driver")) {
					conn = DriverManager.getConnection(hostname, username, password);
					String qExists = "SELECT * from pg_tables where schemaname='public' AND LIKE ?";
					String qMakeDB = "CREATE DATABASE ?";
					PreparedStatement st = conn.prepareStatement(qExists);
					PreparedStatement mkdb = conn.prepareStatement(qMakeDB);
					st.setString(1, database);
					mkdb.setString(1, database);
					
					ResultSet rs = st.executeQuery(qExists);
					if( rs.next() == false ) {
						exists = false;
						
						// Maak de database aan
						mkdb.execute();
						
						// Verbind met de database
						conn = DriverManager.getConnection(hostname+"/"+database, username, password);
						
						exists = true;
					}
				} else if (drivers
						.equalsIgnoreCase("org.apache.derby.jdbc.EmbeddedDriver")) {
					conn = DriverManager.getConnection(hostname+"/"+database+";create=true"+";user="+username+";password"+password);
				}
								
				// Importeer de data vanuit een csv bestand
				CSVImporter importer = new CSVImporter("knmigegevens.txt");
				importer.importMetingen();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return exists;
	}
}
